$(document).ready(function () {

    function disableRegisterButton(on) {
        $("#register-form").data('formValidation').disableSubmitButtons(on);
    }

    $('#register-form').formValidation({
        message:'This value is not valid',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: gettext('The username is required and can\'t be empty')
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: gettext('The password is required and can\'t be empty')
                    }
                }
            },
            password_again: {
                validators: {
                    notEmpty: {
                        message: gettext('The password is required and can\'t be empty')
                    },
                    identical: {
                        field: 'password',
                        message: gettext('The password and its confirm are not the same')
                    }
                }
            },
            real_name: {
                validators: {
                    notEmpty: {
                        message: gettext('Your real name is required and can\'t be empty')
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: gettext('Your email address is required and can\'t be empty')
                    }
                }
            },
            institution: {
                validators: {
                    notEmpty: {
                        message: gettext('Your institution is required and can\'t be empty')
                    }
                }
            }
        }
    })
    .on('success.field.fv',function(e,data){
        // alert(data.fv.getInvalidFields().length);
        if (data.fv.getInvalidFields().length > 0) {
            disableRegisterButton(true);
        }
        else{
            disableRegisterButton(false);
        }
    })
    .on('success.form.fv',function(e){
        e.preventDefault();
        if(!$("#agreed").prop('checked')){
            $.showModal({
                message: 'Please read and agree to the user instructions'
            });
            disableRegisterButton(false);
            return;
        }
        var data={
            username:$("#username").val(),
            password:$("#password").val(),
            email:$("#email").val(),
            real_name:$("#real_name").val(),
            institution:$("#institution").val()
        };
        $.requestJSON({
            url:Urls.resolve('accounts_api_v1:user_list'),
            // url:Urls.resolve('localhost:8000/accounts/api/v1/users/'),
            data:data,
            onSuccess:function(){
                var jump=function(){
                    window.location=Urls.resolve('accounts:login');
                    disableRegisterButton(false);
                };
                // jump();
                $.ajax({
                  type: 'POST',
                  url: '/matcloud/api/account/user/api-signup?_=' + (new Date()).valueOf(),
                  data: {
                    'email': $("#email").val(),
                    'name': $("#username").val(),
                    'pwd': $("#password").val()
                  },
                  success: function(res) {
                    if(res.error) {
                      console.log(res)
                    }
                  },
                  global: false
                }).always(function() {
                  jump();
                });
            },
            onError:function(json){
                console.error(json);
                disableRegisterButton(true);
            },
            onHide:function () {
                disableRegisterButton(false);
            }
        });
        // $.ajax({
        //     type:'POST',
        //     url:'/accounts/api/v1/users/',
        //     // ERROR:root:{'msg': 'Wrong content-type(JSON required)', 'code': 2202}
        //     // Not Acceptable: /accounts/api/v1/users/
        //     // WARNING:django.request:Not Acceptable: /accounts/api/v1/users/
        //
        //     // url:'accounts_api_v1:user_list',
        //     // Not Found: /accounts/register/accounts_api_v1:user_list
        //     // WARNING:django.request:Not Found: /accounts/register/accounts_api_v1:user_list
        //     data:JSON.stringify(data),
        //     dataType: 'json',
        //     contentType: 'application/json',
        //     success:function(){
        //         // alert("success");
        //         var jump=function(){
        //             window.location='/accounts/login/';
        //             disableRegisterButton(false);
        //         };
        //         jump();
        //     },
        //     error:function(json){
        //         // alert(json.data.msg);
        //         // console.log(json);
        //         console.error(json);
        //         disableRegisterButton(true);
        //     },
        //     hide:function(){
        //         disableRegisterButton(false);
        //     }
        // })
    })

});